package codeRandomThoughts.Test99901背包;

public class Solution3 {
    public static int testWeightBagProblem(int[] weight, int[] value, int bagSize) {
        int itemCount = weight.length;
        //dp[i][j]:任取下标0-i的物品放入容量为j的背包所能获得的最大价值为dp[i][j]
        int[] dp = new int[bagSize + 1];


        //i为0,j>=weight[0]时可放入,此时价值为value[0]
        for (int j = weight[0]; j <= bagSize; j++) {
            dp[j] = value[0];
        }

        //递推式
        for (int i = 1; i < itemCount; i++) {
            for (int j = bagSize; j >= weight[i]; j--) {
                dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
            }
        }
        return dp[bagSize];
    }

    public static void main(String[] args) {
        int[] weight = {1, 3, 4};
        int[] value = {15, 20, 30};
        int bagSize = 4;
        System.out.println(testWeightBagProblem(weight, value, bagSize));
    }
}
